package com.plus.demo.config;

import javax.sql.DataSource;

import org.mybatis.plus.annos.MapperScan;
import org.mybatis.plus.boot.DataSourceTransactionManager;
import org.mybatis.plus.core.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@MapperScan(value= {"com.plus.demo.mapper"})
@ComponentScan("com.plus.demo")
public class MybatisConf {

	@Bean
	public DataSource dataSource() {
		DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
		driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
		driverManagerDataSource
				.setUrl("jdbc:mysql://localhost:3308/db_order?serverTimezone=Asia/Shanghai&characterEncoding=utf8");
		driverManagerDataSource.setUsername("root");
		driverManagerDataSource.setPassword("123456");
		return driverManagerDataSource;
	}
	
	@Bean
	public SqlSessionFactory sessionFactory() {
		return new SqlSessionFactory(dataSource(), "mybatis.cfg.xml");
	}
	
	@Bean
	public DataSourceTransactionManager dataSourceTransactionManager() {
		return new DataSourceTransactionManager(sessionFactory());
	}
}
